<template>
  <div class="register-page">
    <van-nav-bar
      title="账号注册"
      left-arrow
      @click-left="back"
    /><br />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        size="large"
        name="username"
        placeholder="请输入账号"
        :rules="[
          { required: true, message: '请填写用户名' },
          { pattern: /^[\w-]{4,16}$/, message: '用户名不得少于4个字符!' },
        ]"
      />
      <van-field
        v-model="password"
        size="large"
        type="password"
        name="password"
        placeholder="请输入密码"
        :rules="[
          { required: true, message: '请填写密码' },
          { pattern: /^\w{6,}$/, message: '密码不得少于6个字符!' },
        ]"
      />
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">注册</van-button>
      </div>
      <router-link class="link" to="/login">已有账号, 去登录~</router-link>
    </van-form>
  </div>
</template>

<script>
import {register} from '@/api/user'
export default {
  name: "register-page",
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
    async onSubmit(values) {
      console.log(values);
      const res = await register(values)
      this.$toast.success(res.description)
      this.$router.push('/login')
    },
    back(){
      this.$router.back()
    }
  },
};
</script>

<style lang="less" scoped>
.link {
  color: #666;
  font-size: 14px;
  margin-left: 120px;
}
</style>
